

clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if AGE1<65 & AGE1>24
keep if INSCOP_Y1==1 // this covers R1-R3 which we are using here.


foreach num of numlist 1 2 3 4 5 {
replace DDNWRK`num'=0 if DDNWRK`num'<0
replace DDNWRK`num'=0 if DDNWRK`num'==.
}


*round length
gen RL1= BEGRFM2 
gen RL2= ENDRFM2-BEGRFM2 if BEGRFY2 ==ENDRFY2
gen RL3=12 - RL1-RL2

* calculate annual days lost (Round 3 falls in year 1 or 2 depending on # of months in each)
gen sick_days=(DDNWRK1+DDNWRK2+DDNWRK3) if count_3_1==1
replace sick_days=(DDNWRK1+DDNWRK2) if count_3_1==0
replace sick_days=sick_days*8/52 // assuming 8 work hours per day, calculate weekly days lost


keep MAR* PRIVAT1 type2 sick_days RL* R* H* labor_force* EMP* HOUR* EMPST* HRWG* ID  year AGE1 AGE3  AGE5 educ_group Sample_Weight DDNWRK*  dp du s count* INSCOV_Y1


gen RL_frac1 = RL1/(RL1+RL2) if  count_3_1==0
gen RL_frac2 = RL2/(RL1+RL2) if  count_3_1==0
replace RL_frac1 = RL1/12 if  count_3_1==1
replace RL_frac2 = RL2/12 if  count_3_1==1
gen RL_frac3=0 if count_3_1==0
replace RL_frac3=RL3/12 if count_3_1==1


gen Hours_avg=(HOUR1*RL_frac1+HOUR2*RL_frac2)-sick_days if count_3_1==0
replace Hours_avg=(HOUR1*RL_frac1+HOUR2*RL_frac2+HOUR3*RL_frac3)-sick_days if count_3_1==1  // USE 

rename AGE1 age
gen agesq=age^2
rename educ_group education


gen type2_new=type2
replace type2_new=2 if type2_new>=2 & type2_new!=.


replace education =2 if education==3 // group s col with col
replace H1=2 if H1==1 // group fair and poor

* workers
qui{
keep if INSCOV_Y1==1

*keep if EMP1==2
reg Hours_avg   i.dp i.du i.s   if H1==2 & education==1 //  fair or poor H
eststo margin: margins i.du#i.s#i.dp, post
est sto h1
reg Hours_avg   i.dp i.du i.s   if H1==3  & education==1 //  good H
eststo margin: margins i.du#i.s#i.dp, post
est sto h2

reg Hours_avg   i.dp i.du i.s   if H1==2  & education==2 // fair or poor H
eststo margin: margins i.du#i.s#i.dp, post
est sto h3
reg Hours_avg   i.dp i.du i.s   if H1==3  & education==2 // good H
eststo margin: margins i.du#i.s#i.dp, post
est sto h4

eststo
	esttab h1 h2 h3 h4  using "${out_tables}/Work_hours_PHI.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers nose nostar not ///
	 b(3) 
	 
	 estout h1 h2 h3 h4  using "${out_tables}/Sick_days.csv", varwidth(25)  nolabel replace  nonumbers 
	 
	eststo clear
}

	
* export to parameters file for model
* workers
qui{
clear 
insheet using "${out_tables}/Sick_days.csv"
drop if v1==""
destring v2 v3 v4 v5, replace
gen shocks=1 if v1=="0.du#0.s#0.dp"
replace shocks=2 if v1=="0.du#0.s#1.dp"
replace shocks=3 if v1=="0.du#1.s#0.dp"
replace shocks=4 if v1=="0.du#1.s#1.dp"
replace shocks=5 if v1=="1.du#0.s#0.dp"
replace shocks=6 if v1=="1.du#0.s#1.dp"
replace shocks=7 if v1=="1.du#1.s#0.dp"
replace shocks=8 if v1=="1.du#1.s#1.dp"
drop v1

reshape long v, i(shocks) j(no)
reshape wide v, i(no) j(shocks)
foreach var of varlist v2-v8 {
replace `var' =v1-`var'
}
replace v1=0
reshape long v, i(no) j(shocks) 
replace v=v/100
sort no shocks
drop no shocks
outsheet using "${data_model}\sick_days.txt", nolabel nonames replace
}


* for non-workers
qui{
clear
use "${data}/MEPS_merged_Health_Whites.dta"
keep if AGE1<65 & AGE1>24
keep if INSCOP_Y1==1 // this covers R1-R3 which we are using here.

foreach num of numlist 1 2 3 4 5 {
replace DDNWRK`num'=0 if DDNWRK`num'<0
replace DDNWRK`num'=0 if DDNWRK`num'==.
}


*round length
gen RL1= BEGRFM2 
gen RL2= ENDRFM2-BEGRFM2 if BEGRFY2 ==ENDRFY2
gen RL3=12 - RL1-RL2

* calculate annual days lost (Round 3 falls in year 1 or 2 depending on # of months in each)
gen sick_days=(DDNWRK1+DDNWRK2+DDNWRK3) if count_3_1==1
replace sick_days=(DDNWRK1+DDNWRK2) if count_3_1==0
replace sick_days=sick_days*8/52 // assuming 8 work hours per day, calculate weekly days lost


keep MAR* PRIVAT1 type2 sick_days RL* R* H* labor_force* EMP* HOUR* EMPST* HRWG* ID  year AGE1 AGE3  AGE5 educ_group Sample_Weight DDNWRK*  dp du s count* INSCOV_Y1

gen RL_frac1 = RL1/(RL1+RL2) if  count_3_1==0
gen RL_frac2 = RL2/(RL1+RL2) if  count_3_1==0
replace RL_frac1 = RL1/12 if  count_3_1==1
replace RL_frac2 = RL2/12 if  count_3_1==1
gen RL_frac3=0 if count_3_1==0
replace RL_frac3=RL3/12 if count_3_1==1


gen Hours_avg=(HOUR1*RL_frac1+HOUR2*RL_frac2)-sick_days if count_3_1==0
replace Hours_avg=(HOUR1*RL_frac1+HOUR2*RL_frac2+HOUR3*RL_frac3)-sick_days if count_3_1==1  

rename AGE1 age
gen agesq=age^2

replace H1=2 if H1==1 // group fair and poor


keep if INSCOV_Y1==1

*keep if EMP1==2
reg Hours_avg   i.dp i.du i.s   if H1==2 
eststo margin: margins i.du#i.s#i.dp, post
est sto h1
reg Hours_avg   i.dp i.du i.s   if H1==3  
eststo margin: margins i.du#i.s#i.dp, post
est sto h2

eststo
	esttab h1 h2   using "${out_tables}/Work_hours_PHI_ALL.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers nose nostar not ///
	 b(3) 
	 
	 estout h1 h2  using "${out_tables}/Sick_days_ALL.csv", varwidth(25)  nolabel replace  nonumbers 
	 
	eststo clear
}

	
	
* export to parameters file for model
* for non-workers
qui{
clear 
insheet using "${out_tables}/Sick_days_ALL.csv"
drop if v1==""
destring v2 v3 , replace
gen shocks=1 if v1=="0.du#0.s#0.dp"
replace shocks=2 if v1=="0.du#0.s#1.dp"
replace shocks=3 if v1=="0.du#1.s#0.dp"
replace shocks=4 if v1=="0.du#1.s#1.dp"
replace shocks=5 if v1=="1.du#0.s#0.dp"
replace shocks=6 if v1=="1.du#0.s#1.dp"
replace shocks=7 if v1=="1.du#1.s#0.dp"
replace shocks=8 if v1=="1.du#1.s#1.dp"
drop v1

reshape long v, i(shocks) j(no)
reshape wide v, i(no) j(shocks)
foreach var of varlist v2-v8 {
replace `var' =v1-`var'
}
replace v1=0
reshape long v, i(no) j(shocks)  
replace v=v/100
sort no shocks
drop no shocks
outsheet using "${data_model}\sick_days_nw.txt", nolabel nonames replace
}
